<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>02拾色器</title>
		<style type="text/css">
			* {
				margin: 0;
				padding: 0;
			}
			body {
				overflow: hidden;
			}
			#firCan {
				display: block;
				border: 1px solid khaki;
				margin: 20px auto;
			}
		</style>
	</head>
	<body>
		<canvas id="firCan" width="1000" height="625">
			您的浏览器版本过低, 不支持Canvas, 请升级!
		</canvas>
	</body>
	<script type="text/javascript">
		// 1, 获取Canvas
		var firCan = document.getElementById("firCan");
		// 2, 获取 2d渲染上下文
		var context = firCan.getContext('2d');
		// 3, 加载图片
		// 创建 img对象
		var imgObj = new Image();
		// 设置 src
		imgObj.src = "img/banner3.png";
		// 图片加载完毕
		imgObj.onload = function() {
			// 图片绘制到 canvas: drawImage()
			context.drawImage(imgObj, 0, 0, 1280, 800, 0, 0, 1000, 625);
			
			// canvas 添加点击事件
			firCan.onclick = function(event) {
				var even = event || window.event;
				var x = even.offsetX;
				var y = even.offsetY;
				var getImageData = context.getImageData(x, y, 1, 1);
				var pixels = getImageData.data;
				console.log(getImageData);
				var color = "rgb(" + pixels[0] + "," + pixels[1] + "," + pixels[2] + ")";
				document.body.style.background = color;
			}
		}
		
		
		
	</script>
</html>
